package com.xgkj.diyiketang.fragment;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.os.Bundle;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xgkj.diyiketang.ProtocolConst;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.MallOrderActivity;
import com.xgkj.diyiketang.activity.MyAddressActivity;
import com.xgkj.diyiketang.activity.YuanwanglistActivity;
import com.xgkj.diyiketang.activity.data.MallGoodsAdapter;
import com.xgkj.diyiketang.data.GoodsInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.MallListTask;
import com.xgkj.diyiketang.util.PalLog;
import com.xgkj.diyiketang.weidget.MyScrollView;
import com.xgkj.diyiketang.weidget.NoScrollGridView;

import java.util.ArrayList;
import java.util.List;

public class MallFragment extends BaseFragment implements View.OnClickListener,MyScrollView.OnScrollListener {

    private Context mContext;
    private TextView orderBtn;
    private TextView addrBtn;
    private NoScrollGridView mGridView;
    private MallGoodsAdapter mAdapter;
    private MyScrollView mScrollView;
    private TextView back;
    private int currentPage = 1;
    private int size = 10;
    private int mallTaskId = -1;
    private TextView mNeedFlour;
    private TextView yuanwang;
    private RelativeLayout topLayout;


    @Override
    protected int getContentViewId() {
        return 0;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.mall, null);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mContext = getActivity();
        initView(view);
    }



    private void initView(View view){
        topLayout = (RelativeLayout) view.findViewById(R.id.titlebar);
        topLayout.setVisibility(View.GONE);

        mScrollView = (MyScrollView) view.findViewById(R.id.seller_scrollView);
        mScrollView.setOnScrollListener(this);
        orderBtn = (TextView) view.findViewById(R.id.myorder);
        orderBtn.setOnClickListener(this);
        orderBtn.setFocusable(true);
        orderBtn.setFocusableInTouchMode(true);
        orderBtn.requestFocus();
        addrBtn = (TextView) view.findViewById(R.id.myaddr);
        addrBtn.setOnClickListener(this);
        yuanwang = (TextView) view.findViewById(R.id.yuanwang);
        yuanwang.setOnClickListener(this);
        mGridView = (NoScrollGridView) view.findViewById(R.id.seller_video_list);
        mAdapter = new MallGoodsAdapter(mContext);
        mGridView.setAdapter(mAdapter);
        mNeedFlour = (TextView) view.findViewById(R.id.need_flour);
        getData();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden){
            if (mAdapter.getCount() == 0) {
                currentPage = 1;
                getData();
            }
        }
    }

    private void getData() {
        if (currentPage == 0) {
            return;
        }
        if (mallTaskId != -1) {
            return;
        }
        MallListTask task = new MallListTask(size,currentPage);
        task.addListener(new MallTaskEventListener(mContext));
        mallTaskId = getEatShowApp().getTaskManager().addTask(task);
    }

    class MallTaskEventListener extends ShowToastTaskEventListener{
        public MallTaskEventListener(Context context) {
            super(context);
        }

        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if (taskId == mallTaskId) {
                mallTaskId = -1;
                if (currentPage == 1) {
                    mAdapter.clearData();
                }
                List<GoodsInfo> mfoodlist = new ArrayList<GoodsInfo>();
                int pageCounts = 0;
                int currentPages = 0;
                int nextPages = 0;
                if (data != null) {
                    JSONObject jsonString = (JSONObject) data;
                    String flour = jsonString.getString("flowCount");
                    mNeedFlour.setText(flour);
                    currentPages = jsonString.getIntValue(ProtocolConst.CURRENT_PAGE);
                    pageCounts = jsonString.getIntValue(ProtocolConst.PAGE_COUNT);
                    nextPages = currentPage >= pageCounts ? 0 : (currentPage + 1);
                    mfoodlist = JSON.parseArray(jsonString.getString(ProtocolConst.LIST_TAG), GoodsInfo.class);
                }
                if(mfoodlist != null && mfoodlist.size()>0){
                    mAdapter.updateData(mfoodlist);
                }
                currentPage = nextPages;
                PalLog.e("dangqiangye",currentPage+"");

            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            if (taskId == mallTaskId) {
                mallTaskId = -1;
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.myorder:
                Intent oIntent = new Intent(mContext,MallOrderActivity.class);
                startActivity(oIntent);
                break;

            case R.id.myaddr:
                Intent aIntent = new Intent(mContext,MyAddressActivity.class);
                startActivity(aIntent);
                break;
            case R.id.yuanwang:
                Intent yuanwang_intent = new Intent(mContext,YuanwanglistActivity.class);
                startActivity(yuanwang_intent);
                break;
        }
    }

    @Override
    public void onScroll() {
        getData();
    }

}
